package co.fitstart.admin.web.controller.finance;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.DataService;
import co.fitstart.admin.service.OrderService;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.finance.ExchangeRate;
import co.fitstart.entity.order.Order;
import co.fitstart.entity.order.PaymentStatus;
import co.fitstart.entity.principal.Principal;

@Controller
@RequestMapping("/sales-orders")
@MenuSetting("menu-finance")
public class SaleController {
    
    @Autowired
    private OrderService orderService;
    
    @Autowired
    private PrincipalService principalService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<ExchangeRate> exchangeRateService;
    
    @Autowired
    private SiteService siteService;
    
    /**
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-sale-management")
    public ModelAndView list(@RequestParam(name = "export", required = false, defaultValue = "false") boolean export,
                             @RequestParam(name = "paid", required = false, defaultValue = "true") boolean paid,
                             Page page, 
                             HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("finance/sales-order-list");
        
        PaymentStatus[] paymentStatus = null;
        boolean payoffer = false;
        if(paid) {
            paymentStatus = new PaymentStatus[]{PaymentStatus.PAID};
        } else {
            paymentStatus = new PaymentStatus[]{PaymentStatus.UNPAID, PaymentStatus.PAID};
            payoffer = true;
        }
        List<Order> orders = orderService.list(export, payoffer, paymentStatus, page);
        modelAndView.addObject("orders", orders);
        
        modelAndView.addObject("page", page);
        modelAndView.addObject("export", export);
        modelAndView.addObject("paid", paid);
        
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param paid
     * @return
     */
    @RequestMapping(value = "/export-view", method = RequestMethod.GET)
    public ModelAndView export(@RequestParam("paid") boolean paid) {
        ModelAndView modelAndView = new ModelAndView("finance/sales-order-export");
        
        List<String> cities = siteService.loadAllCity();
        
        modelAndView.addObject("paid", paid);
        modelAndView.addObject("cities", cities);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param login
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(@Secure Login login,
                                 @RequestParam("paid") boolean paid,
                                 @RequestParam("city") String city) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("sales-order.xls"));
        
        PaymentStatus[] paymentStatus = null;
        boolean payoffer = false;
        if(paid) {
            paymentStatus = new PaymentStatus[]{PaymentStatus.PAID};
        } else {
            paymentStatus = new PaymentStatus[]{PaymentStatus.UNPAID, PaymentStatus.PAID};
            payoffer = true;
        }
        
        // list all unexport student
        List<Order> orders = orderService.list(payoffer, paymentStatus, city);
        modelAndView.addObject("orders", orders);
        
        Map<Long, Principal> managers = principalService.listSiteManager();
        modelAndView.addObject("managers", managers);
        
        List<ExchangeRate> exchangeRates = exchangeRateService.listAll(ExchangeRate.class);
        modelAndView.addObject("exchangeRate", exchangeRates.isEmpty() ? null : exchangeRates.get(0));
        
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        modelAndView.addObject("fmt", format);
        
        modelAndView.addObject("paid", paid);
        
        // update student's exportArchives
        if(!orders.isEmpty()) {
            List<Long> orderIds = CollectionUtils.extractPropertyList(orders, "id");
            orderService.exportSales(orderIds, login.getId());
        }
        
        return modelAndView;
    }

}
